package com.app.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.main.SdUtilBase;
import com.core.lib.utils.main.StringUtilBase;
import com.lepu.pasm.R;

import org.apache.http.util.EncodingUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class SdLocal {

	public static final String LEPU_FOLDER = "/lepu/pasm";
	public static String LEPU_PATH = "";

	public static String LEPU_PATH_SD = "";
	public static String DEFAULT_USER = "-1";

	/**
	 * 外置存储
	 * 
	 * @param context
	 * @return
	 */
	public static String getLepuRootPathSD(Context context) {
		if (StringUtilBase.stringIsEmpty(LEPU_PATH_SD)) {
			String path = SdUtilBase.getStorgePath(context);
			if (!StringUtilBase.stringIsEmpty(path)) {
				LEPU_PATH_SD = path + LEPU_FOLDER;
			}
		}
		return LEPU_PATH_SD;
	}

	// ==================================================================================
	/**
	 * 内置app存储
	 * 
	 * @param context
	 * @return
	 */
	private static String getLepuRootPath(Context context) {
		if (StringUtilBase.stringIsEmpty(LEPU_PATH)) {
			String path = context.getFilesDir().getAbsolutePath();
			// String path = SdUtilBase.getStorgePath(context);
			if (!StringUtilBase.stringIsEmpty(path)) {
				LEPU_PATH = path + LEPU_FOLDER;
			}

			//空间不足时，
//			try {
//				if(!SdUtilBase.checkIfFreeSpace(LEPU_PATH)) {
//					LEPU_PATH = getLepuRootPathSD(context);
//				}
//			}catch (Exception e){
//			}
		}
		return LEPU_PATH;
	}

	// ===================================================================
	// all user
	// 获取download folder
	private static String getDownloadFolder(Context context) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPathSD(context), "Download");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	// 获取temp folder
	public static String getTempFolder(Context context) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPathSD(context), "Temp");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	// 获取头像 缓存folder
	public static String getHeadIconFolder(Context context) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPathSD(context), "HeadIcon");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	private static String getCacheImageFolder(Context context) {
		String iconFolder = StringUtilBase.combinePath(
				getLepuRootPath(context), "/CacheImage");

		File file = new File(iconFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return iconFolder;
	}

	private static String getCacheImageFolder(Context context, String userId) {
		String iconFolder = StringUtilBase.combinePath(
				getLepuRootPath(context), userId + "/CacheImage");

		File file = new File(iconFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return iconFolder;
	}

	private static String getCacheDataFolder(Context context) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPathSD(context), "/CacheData");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	private static String getCacheDataFolder(Context context, String userId) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPathSD(context), userId + "/CacheData");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	private static String getChatImageFolder(Context context, String UserID) {
		String fileFolder = StringUtilBase.combinePath(
				getChatFolder(context), UserID + "/ChatImage");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	private static String getChatVoiceFolder(Context context, String UserID) {
		String fileFolder = StringUtilBase.combinePath(
				getChatFolder(context), UserID + "/ChatVoice");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	private static String getChatVideoFolder(Context context, String UserID) {
		String fileFolder = StringUtilBase.combinePath(
				getChatFolder(context), UserID + "/ChatVideo");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	public static String getChatFolder(Context context) {
		String fileFolder = StringUtilBase.combinePath(
				getLepuRootPathSD(context), "/Chat");

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	// =====================================================================================
	// 术后天天知文件夹
	public static String getCacheDataSHTTZFolder(Context context) {
		String fileFolder = getCacheDataFolder(context) + "/shuhou";

		File file = new File(fileFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return fileFolder;
	}

	public static String getCacheImagePath(Context context, String userId,
			String flag) {
		return StringUtilBase.combinePath(getCacheImageFolder(context, userId),
				flag + ".jpg");
	}

	public static String getCacheImagePath(Context context, String flag) {
		return StringUtilBase.combinePath(getCacheImageFolder(context), flag
				+ ".jpg");
	}

	/**
	 * 获取用户的头像
	 * 
	 * @param context
	 * @param userId
	 * @return
	 */
	public static String getUserPath(Context context, String userId) {
		return StringUtilBase.combinePath(getTempFolder(context), userId
				+ ".jpg");
	}

	public static String getAppIconPath(Context context) {
		String appIconPath = StringUtilBase.combinePath(getTempFolder(context),
				"app_icon" + ".png");
		if (!new File(appIconPath).exists()) {
			Bitmap iconBitmap = ImageUtilBase.getBitmapByResId(context,
					R.drawable.app_icon);
			ImageUtilBase.saveImageToSd(CompressFormat.PNG, iconBitmap,
					appIconPath);
		}
		return appIconPath;
	}

	public static String getTempPhotoPath(Context context, String flag) {
		return StringUtilBase
				.combinePath(getTempFolder(context), flag + ".jpg");
	}

	public static String getTempPhotoPath(Context context) {
		return StringUtilBase.combinePath(getTempFolder(context), "temp.jpg");
	}

	public static String getTempFilePath(Context context, String flag) {
		return StringUtilBase
				.combinePath(getTempFolder(context), flag + ".txt");
	}

	public static String getDebugLogPath(Context context) {
		return StringUtilBase.combinePath(getTempFolder(context), "debug.log");
	}

	public static String getDatabasePath(Context context, String dbName) {
		return context.getDatabasePath(dbName).getPath();
	}

	public static String getDownloadApkPath(Context context, String name) {
		return StringUtilBase.combinePath(getDownloadFolder(context), name
				+ ".apk");
	}

	public static String getDownloadZipPath(Context context, String name) {
		return StringUtilBase.combinePath(getDownloadFolder(context), name
				+ ".zip");
	}

	public static String getShuHouTianTianZhiPath(Context context, String name) {
		return StringUtilBase.combinePath(getCacheDataSHTTZFolder(context),
				name + ".zip");
	}

	// 读SD中的文件
	public static String readFileSdcardFile(String fileName) throws IOException {
		String res = "";
		try {
			FileInputStream fin = new FileInputStream(fileName);
			int length = fin.available();
			byte[] buffer = new byte[length];
			fin.read(buffer);
			res = EncodingUtils.getString(buffer, "UTF-8");
			fin.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	public static String getChatImagePath(Context context, String userId,
										  String name) {
		return StringUtilBase.combinePath(
				getChatImageFolder(context, userId), name + ".jpg");
	}

	public static String getChatVoicePath(Context context, String userId,
			String name) {
		return StringUtilBase.combinePath(
				getChatVoiceFolder(context, userId), name + ".amr");
	}
	
	public static String getChatVideoPath(Context context, String userId,
			String name) {
		return StringUtilBase.combinePath(
				getChatVideoFolder(context, userId), name + ".mp4");
	}


}
